import { defineStore } from 'pinia'
import { ref } from 'vue'

export const liaotStore = defineStore(
  'liaotStore',
  () => {
    const name = ref<any>('')
    const userid = ref<any>('')
    const toid = ref<any>('')
    const uid = ref<any>('')
    const setuserid = (val: any) => {
      console.log('userid被存进pinia')
      userid.value = val
    }
    const settoid = (val: any) => {
      console.log('toid被存进pinia')
      toid.value = val
    }
    const setuid = (val: any) => {
      console.log('uid被存进pinia')
      uid.value = val
    }
    const setname = (val: any) => {
      console.log('userinfo被存进pinia')
      name.value = val
    }

    // 清理用户信息，退出时使用
    const clearname = () => {
      name.value = undefined
    }
    // 要返回
    return {
      name,
      clearname,
      setname,
      setuserid,
      userid,
      settoid,
      toid,
      setuid,
      uid,
    }
  },
  // TODO: 持久化   这个主要在pinia里设置后就会自动存到小程序配置里。
  {
    // 网页端配置
    // persist: true,
    // 小程序端配置
    persist: {
      storage: {
        getItem(key) {
          return uni.getStorageSync(key)
        },
        setItem(key, value) {
          uni.setStorageSync(key, value)
        },
      },
    },
  },
)
